﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using KCMS.Areas.Admin.Engine;
using StructureMap;
using KCMS.BusinessLogic.Models;
using KCMS.Areas.Admin.Models;

namespace KCMS.Areas.Admin.Controllers
{
    public class ProductController : Controller
    {
        private IProductService productService = ObjectFactory.GetInstance<IProductService>();
        private IProductCategoryService pCatService = ObjectFactory.GetInstance<IProductCategoryService>();
        private GenderListGenerator<ProductCategory> genderGenerator = new GenderListGenerator<ProductCategory>();
        private GenderListGenerator<Brend> brendListGenerator = new GenderListGenerator<Brend>();
        private IBrendService brendService = ObjectFactory.GetInstance<IBrendService>();
        public ActionResult Index()
        {
            ViewData["categories"] = genderGenerator.getGenderListWithEmptyOption(pCatService.getAll());
            var model = (from t in productService.getAll()
                         orderby t.Index descending
                         select t).ToList();
            return View(model);
        }
        [AcceptVerbs("POST")]
        public PartialViewResult GetByArticle(String article)
        {
            IEnumerable<Product> products =(from t in productService.searchByArticle(article)
                                                orderby t.Index descending
                                                select t).ToList();
            return PartialView("_ProductList", products);
        }
        public PartialViewResult GetNotPublic()
        {
            IEnumerable<Product> products = (from t in productService.getAll()
                                             where t.Published.Equals(false)
                                             orderby t.Index descending
                                             select t).ToList();
            return PartialView("_ProductList", products);
        }
        public ActionResult GetByCategory(Guid categoryId)
        {
            List<Product> products = new List<Product>();
            if (categoryId.Equals(Guid.Empty))
            {
                products.AddRange((from t in productService.getAll()
                                select t).ToList());
            }
            else
            {
                products.AddRange((from t in productService.getAll()
                                where t.CategoryId.Equals(categoryId)
                                select t).ToList());
            }
            ViewData["categories"] = genderGenerator.getGenderList(pCatService.getAll());
            return PartialView("_ProductList", products);
        }
        [HttpGet]
        public ActionResult Create()
        {
            var model = new ProductEditorModel();
            model.CategoryList = genderGenerator.getGenderList(pCatService.getAll(), Guid.Empty);
            model.BrendList = brendListGenerator.getGenderList(brendService.getAll(), Guid.Empty);
            model.Product.Index = productService.getLastIndex() + 1;
            model.Product.Published = true;
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(ProductEditorModel model)
        {
            productService.save(model.Product);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            Product product = productService.getById(id);
            var model = new ProductEditorModel(product);
            model.CategoryList = genderGenerator.getGenderList(pCatService.getAll(), model.Product.CategoryId);
            model.BrendList = brendListGenerator.getGenderList(brendService.getAll(), Guid.Empty);
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(ProductEditorModel model)
        {

            productService.update(model.Product);
            return RedirectToAction("Index");
        }
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            Product model = productService.getById(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Delete(Product model)
        {
            productService.delete(model.Id);
            return RedirectToAction("Index");
        }

    }
}
